/**
 * Copyright 19.3.11 (c) DataLite, spol. s r.o. All rights reserved.
 * Web: http://www.datalite.cz    Mail: info@datalite.cz 
 */
package cz.datalite.task.model;

import cz.datalite.helpers.EqualsHelper;
import cz.datalite.helpers.JpaEqualHelper;
import org.hibernate.annotations.ForeignKey;
import org.hibernate.annotations.Type;

import javax.persistence.*;
import javax.validation.constraints.Future;
import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.util.Date;

/**
 * Entity mapped to a database table.
 *
 * @author Jiri Bubnik
 */
@Entity
public class Task implements Serializable {

    /** Primary key (autogenerated). */
    @Id
    @GeneratedValue
    private Long idTask;

    /** Task name */
    @NotNull
    private String name;

    /** Task description */
    private String description;

    /** When is the due of this task */
    @Temporal(value = TemporalType.TIMESTAMP)
    @Future(message = "The task due must be set into future.")
    private Date taskDue;

    /** Priority of this task **/
    @Enumerated(value = EnumType.STRING)
    @NotNull
    private Priority priority = Priority.NORMAL;

    /** Category of this task **/
    @ManyToOne
    @ForeignKey(name = "FK_TASK_CATEGORY")
    @NotNull
    private Category idCategory;

    /** Is the task done? **/
    @Type(type = "yes_no")  @Column(columnDefinition="CHAR(1) DEFAULT 'Y'")
    boolean done = false;

    /**
     * ************** Serializable / Equals / Hash / toString ****************
     */

    private static final long serialVersionUID = 31012010L;

    @Override
    public boolean equals(Object obj) {
        return this == obj || (JpaEqualHelper.isEntitySameClass(this, obj) &&
                EqualsHelper.isEquals(this.idTask, ((Task) obj).getIdTask()));
    }

    @Override
    public int hashCode() {
        return this.idTask == null ? super.hashCode() : this.idTask.hashCode();
    }

    @Override
    public String toString() {
        return "cz.datalite.task.model.Task[id=" + this.idTask + "][val=" + name + "]";
    }

    /**
     * ************************ SETTERS & GETTERS ***************************
     */

    public Long getIdTask() {
        return idTask;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Date getTaskDue() {
        return taskDue;
    }

    public void setTaskDue(Date taskDue) {
        this.taskDue = taskDue;
    }

    public Priority getPriority() {
        return priority;
    }

    public void setPriority(Priority priority) {
        this.priority = priority;
    }

    public Category getIdCategory() {
        return idCategory;
    }

    public void setIdCategory(Category idCategory) {
        this.idCategory = idCategory;
    }

    public boolean isDone() {
        return done;
    }

    public void setDone(boolean done) {
        this.done = done;
    }
}